package com.shiwaixiangcun.monitor.controller;

import com.rabbit.spring.data.search.SearchOperator;
import com.rabbit.spring.data.search.Searchable;
import com.shiwaixiangcun.core.plugin.web.validator.ConstraintType;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraint;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraints;

import com.shiwaixiangcun.monitor.domain.WorkOrderCategory;
import com.shiwaixiangcun.monitor.service.WorkOrderCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * Created by Administrator on 2017-11-23.
 */
@Controller
@RequestMapping("/category")
public class WorkOrderCategoryController {

    @Autowired
    private WorkOrderCategoryService workOrderCategoryService;

    /**
     * 新增
     */
    @ParameterConstraints({
            @ParameterConstraint(name = "name", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "名称不能为空")),
            @ParameterConstraint(name = "parentId", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "parentId不能为空"))
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addCategory(String name, Long parentId) {
        workOrderCategoryService.addWorkOrderCategory(name, parentId);
        return "SUCCESS:新建成功";
    }

    /**
     * 移除
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public String removeCategory(Long id) {
        workOrderCategoryService.removeWorkOrderCategory(id);
        return "SUCCESS:删除成功";
    }

    /**
     * 查询
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<WorkOrderCategory> getListData() {
        Searchable searchable = Searchable.newSearchable();
        searchable.addSearchFilter("status", SearchOperator.eq, "ENABLE");
        WorkOrderCategory workOrderCategory = workOrderCategoryService.findByParentId(0L);
        return workOrderCategoryService.findAllByName(searchable, workOrderCategory);
    }


    @RequestMapping(value = "/children", method = RequestMethod.GET)
    public WorkOrderCategory children() {
        return workOrderCategoryService.findChildren(1L, false);
    }


}
